package com.utopia.keyvalue.store.impl.disk;

import java.nio.ByteBuffer;

import com.utopia.keyvalue.utils.ByteSerializer;

/**
 * @author yanjiaqiang
 * @date 2011-3-17
 * @version $Id: IndexEntry.java 5 2011-03-22 09:23:42Z JiaqiangYan@gmail.com $
 */
public class IndexEntry implements ByteSerializer {

	public static final int LENGTH = 17;

	/**
	 * 该index在文件中偏移量
	 */
	private int offset;
	/**
	 * true : available,false: unavailable
	 */
	private boolean available;

	/**
	 * key's hash value
	 */
	private int keyhash;
	/**
	 * 本index对应的数据所存放的文件标识
	 * 
	 */
	private int datafileIndex;
	/**
	 * 本index对应的value存放在datafile的偏移量
	 */
	private int datafilePosOffset;

	public IndexEntry() {
	}

	public IndexEntry(int offset, boolean available, int keyhash, int datafileIndex, int datafilePosOffset) {
		super();
		this.offset = offset;
		this.available = available;
		this.keyhash = keyhash;
		this.datafileIndex = datafileIndex;
		this.datafilePosOffset = datafilePosOffset;
	}

	public boolean isAvailable() {
		return available;
	}

	public void setAvailable(boolean available) {
		this.available = available;
	}

	public int getDatafileIndex() {
		return datafileIndex;
	}

	public void setDatafileIndex(int datafileIndex) {
		this.datafileIndex = datafileIndex;
	}

	public int getDatafilePosOffset() {
		return datafilePosOffset;
	}

	public void setDatafilePosOffset(int datafilePosOffset) {
		this.datafilePosOffset = datafilePosOffset;
	}

	public int getKeyhash() {
		return keyhash;
	}

	public void setKeyhash(int keyhash) {
		this.keyhash = keyhash;
	}

	public int getOffset() {
		return offset;
	}

	public void setOffset(int offset) {
		this.offset = offset;
	}

	@Override
	public byte[] toBytes() {
		ByteBuffer buf = ByteBuffer.allocate(LENGTH);
		buf.putInt(offset);
		buf.put(available ? (byte) 0 : (byte) 1);
		buf.putInt(keyhash);
		buf.putInt(datafileIndex);
		buf.putInt(datafilePosOffset);
		return buf.array();
	}

	@Override
	public void fromBytes(byte[] bs) {
		ByteBuffer buf = ByteBuffer.wrap(bs);
		this.offset = buf.getInt();
		byte b = buf.get();
		this.available = (b == (byte) 0 ? true : false);
		this.keyhash = buf.getInt();
		this.datafileIndex = buf.getInt();
		this.datafilePosOffset = buf.getInt();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (available ? 1231 : 1237);
		result = prime * result + datafileIndex;
		result = prime * result + datafilePosOffset;
		result = prime * result + keyhash;
		result = prime * result + offset;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		IndexEntry other = (IndexEntry) obj;
		if (available != other.available)
			return false;
		if (datafileIndex != other.datafileIndex)
			return false;
		if (datafilePosOffset != other.datafilePosOffset)
			return false;
		if (keyhash != other.keyhash)
			return false;
		if (offset != other.offset)
			return false;
		return true;
	}

}
